package com.ruoyi.core.config;

import com.ruoyi.core.task.DefaultScheduledTaskRegistrar;
import com.ruoyi.core.task.JsPluginRefreshJob;
import com.ruoyi.core.task.JungleScoutCookieRefreshJob;
import com.ruoyi.core.task.MerchantWordCookieRefreshOnOffUpdateJob;
import com.ruoyi.core.task.factory.PeriodicTriggerTaskFactory;
import com.ruoyi.core.task.factory.TriggerTaskBean;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.Assert;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description:
 * @Company: 北京卡拉卡尔股份技术有限公司
 * @Author: Darren Ho
 * @Date: 10/20/2020 16:19
 */
@EnableScheduling
@Configuration
public class CustomSchedulingTaskConfig {


    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private MerchantWordCookieRefreshOnOffUpdateJob merchantWordCookieRefreshOnOffUpdateJob;

    @Autowired
    private JungleScoutCookieRefreshJob junglescoutCookieRefreshJob;

    @Autowired
    private JsPluginRefreshJob jsPluginRefreshJob;

    @Autowired
    private PeriodicTriggerTaskFactory triggerTaskFactory;


//    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
        executor.setPoolSize(10);
        executor.setThreadNamePrefix("pm-task-scheduler-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //调度器shutdown被调用时等待当前被调度的任务完成
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //等待时长
        executor.setAwaitTerminationSeconds(60);
        return executor;
    }

//    @Bean
    public DefaultScheduledTaskRegistrar defaultScheduledTaskRegistrar() {
        DefaultScheduledTaskRegistrar defaultScheduledTaskRegistrar = new DefaultScheduledTaskRegistrar(taskScheduler());
        riggerTaskRegister(defaultScheduledTaskRegistrar);
        return defaultScheduledTaskRegistrar;
    }

    private void riggerTaskRegister(DefaultScheduledTaskRegistrar defaultScheduledTaskRegistrar) {

        //  配置定时任务
        //1） 亚马逊网站 ，jungle scout  ，js  plugin
        TriggerTaskBean merchantWordTriggerTask = triggerTaskFactory.getTriggerTask("MERCHANT_WORD_PRODUCT_ID", merchantWordCookieRefreshOnOffUpdateJob.getCookieRefreshTimeInterval());
        defaultScheduledTaskRegistrar.addTriggerTask(merchantWordTriggerTask.getTaskId(), merchantWordTriggerTask.getTriggerTask());

        //  jungle scout
        TriggerTaskBean jungleScoutTriggerTask = triggerTaskFactory.getTriggerTask("JUNGLE_SCOUT_PRODUCT_ID", junglescoutCookieRefreshJob.getCookieRefreshTimeInterval());
        defaultScheduledTaskRegistrar.addTriggerTask(jungleScoutTriggerTask.getTaskId(), jungleScoutTriggerTask.getTriggerTask());
        // js plugin
        TriggerTaskBean jsTriggerTask = triggerTaskFactory.getTriggerTask("JS_PLUGIN_PRODUCT_ID", jsPluginRefreshJob.getCookieRefreshTimeInterval());
        defaultScheduledTaskRegistrar.addTriggerTask(jsTriggerTask.getTaskId(), jsTriggerTask.getTriggerTask());

        // 启动监控任务
        new Timer().scheduleAtFixedRate(new CustomTimerTask(defaultScheduledTaskRegistrar), 10 * 1000, 10 * 1000);
    }


    private class CustomTimerTask extends TimerTask {
        private DefaultScheduledTaskRegistrar defaultScheduledTaskRegistrar;

        public CustomTimerTask(DefaultScheduledTaskRegistrar defaultScheduledTaskRegistrar) {
            this.defaultScheduledTaskRegistrar = defaultScheduledTaskRegistrar;
        }

        public void run() {

            // 亚马逊
            String merchantWordCookieRefreshTimeInterval = sysConfigService.selectConfigByKey("COOKIE_REFRESH_TIME_INTERVAL");
            Assert.hasText(merchantWordCookieRefreshTimeInterval, "cookie refresh time interval must has text");
            Integer merchantWordCookieRefreshTimeIntervalInter = Integer.parseInt(merchantWordCookieRefreshTimeInterval);
            if (!merchantWordCookieRefreshTimeIntervalInter.equals(merchantWordCookieRefreshOnOffUpdateJob.getCookieRefreshTimeInterval())) {
                merchantWordCookieRefreshOnOffUpdateJob.setCookieRefreshTimeInterval(merchantWordCookieRefreshTimeIntervalInter);
                TriggerTaskBean triggerTask = triggerTaskFactory.getTriggerTask("MERCHANT_WORD_PRODUCT_ID", merchantWordCookieRefreshOnOffUpdateJob.getCookieRefreshTimeInterval());
                //
                defaultScheduledTaskRegistrar.resetTriggerTask(triggerTask.getTaskId(), triggerTask.getTriggerTask());
            }


            //
            String jungleScoutCookieRefreshTimeInterval = sysConfigService.selectConfigByKey("JUNGLE_SCOUT_COOKIE_REFRESH_TIME_INTERVAL");
            Assert.hasText(jungleScoutCookieRefreshTimeInterval, "jungle scout cookie refresh corn must has text");

            Integer jungleScoutCookieRefreshTimeIntervalInter = Integer.parseInt(jungleScoutCookieRefreshTimeInterval);
            if (!jungleScoutCookieRefreshTimeIntervalInter.equals(junglescoutCookieRefreshJob.getCookieRefreshTimeInterval())) {
                junglescoutCookieRefreshJob.setCookieRefreshTimeInterval(jungleScoutCookieRefreshTimeIntervalInter);
                TriggerTaskBean triggerTask = triggerTaskFactory.getTriggerTask("JUNGLE_SCOUT_PRODUCT_ID", junglescoutCookieRefreshJob.getCookieRefreshTimeInterval());
                defaultScheduledTaskRegistrar.resetTriggerTask(triggerTask.getTaskId(), triggerTask.getTriggerTask());
            }


            //

            String jsCookieRefreshTimeInterval = sysConfigService.selectConfigByKey("JS_PLUGIN_COOKIE_REFRESH_TIME_INTERVAL");
            Assert.hasText(jsCookieRefreshTimeInterval, "js plugin cookie refresh corn must has text");

            Integer jsCookieRefreshTimeIntervalInter = Integer.parseInt(jsCookieRefreshTimeInterval);
            if (!jsCookieRefreshTimeIntervalInter.equals(jsPluginRefreshJob.getCookieRefreshTimeInterval())) {
                jsPluginRefreshJob.setCookieRefreshTimeInterval(jsCookieRefreshTimeIntervalInter);

                TriggerTaskBean triggerTask = triggerTaskFactory.getTriggerTask("JS_PLUGIN_PRODUCT_ID", jsPluginRefreshJob.getCookieRefreshTimeInterval());
                defaultScheduledTaskRegistrar.resetTriggerTask(triggerTask.getTaskId(), triggerTask.getTriggerTask());
            }
        }

    }


}
